clear all
set type double
set varabbrev on, perm

*** FILL IN MAIN PATH TO RUN CODE ***
global dropbox "..."

global folder "$dropbox/_0_bjps_replication"
global in "$folder/in"
global out "$folder/out"
global proc "$folder/proc"

global graph "$out/graph"
global table "$out/table"
global temp "`c(tmpdir)'"

**************
import excel using "$proc/sbo handcoding republican.xlsx", case(l)firstrow clear
ren raclassification cat_1
ren ndcategory cat_2
ren rdcategory cat_3
ren thcategory cat_4
sa "$temp/cat", replace

u "$in/2024.08.10 Small Business Owner Survey (Valid Responses).dta", clear
ren PROLIFIC_PID, lower
merge 1:1 prolific_pid using "$temp/cat", assert(3)nogen
keep if pass_quality == 1

drop if mi(party_id_6pt)
g label = "Democrat" if party_id_6pt < 0.5
replace label = "Republican" if party_id_6pt > 0.5

g count = 1
bysort label:egen tot = total(count) //denominator

keep prolific_pid cat_* tot label count
reshape long cat_, i(prolific_pid tot label count)j(rank)
drop if mi(cat_)

collapse(rawsum)count, by(tot cat_ label)
g pct = count/tot*100
sort label cat_

replace cat_ = "Anything Else" if regexm(cat_, "Anything Else")
replace cat_ = "Other Economic Issues" if regexm(cat_, "Other Economic Issues")

g cat_rank = cat_ == "Regulatory Policy Views and Size of Government "
replace cat_rank = 1.5 if cat_ == "Taxes"
replace cat_rank = 2 if cat_ == "Other Economic Issues"
replace cat_rank = 2.5 if regexm(cat_,"Republicans are good for business")
replace cat_rank = 3 if cat_ == "Social Issues"
replace cat_rank = 4 if cat_ == "Other Policy Issues"
replace cat_rank = 5 if cat_ == "Identity / Group Affiliations"
replace cat_rank = 6 if cat_ == "Anything Else"

sort label cat_rank

capture ssc install listtex

*** table ***
format pct* %12.1fc

	g tab = "\begin{tabular}{lr}" in 1
	g top = "\toprule" in 1
	*Midrule
	g mid = " \midrule" in 1 
	g hline = " \hline" in 1 
	*Bottomrule
	g bot = "\bottomrule" in 1
	*End
	g end = "\end{tabular}" in 1
	*Panel

	g title1 = " & Percent \\" in 1 
	
	g row0 = "\textit{Democrats}&  \\" in 1
	g row1 = "\textit{Republicans}& \\" in 1

	
local filename = "3_percep_sbo_r"

	listtex tab if _n == 1 using "$table/`filename'.tex", replace rstyle(none)
	listtex hline if _n == 1, appendto("$table/`filename'.tex") rstyle(none)
	listtex title1 if _n == 1, appendto("$table/`filename'.tex") rstyle(none)
	listtex hline if _n == 1, appendto("$table/`filename'.tex") rstyle(none)
	
	listtex row0 if _n == 1, appendto("$table/`filename'.tex") rstyle(none)
	listtex cat_ pct if label == "Democrat", appendto("$table/`filename'.tex") rstyle(tabular)
	listtex hline if _n == 1, appendto("$table/`filename'.tex") rstyle(none)
	
	listtex row1 if _n == 1, appendto("$table/`filename'.tex") rstyle(none)
	listtex cat_ pct if label == "Republican", appendto("$table/`filename'.tex") rstyle(tabular)
	listtex hline if _n == 1, appendto("$table/`filename'.tex") rstyle(none)

	listtex bot if _n == 1, appendto("$table/`filename'.tex") rstyle(none)	
	listtex end if _n == 1, appendto("$table/`filename'.tex") rstyle(none)
